package com.test.spring.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

/**
 * @Author ll
 * @Date 2017/6/15 10:51
 */
public class MD5Util {
    public static void main(String[] args) {
        String a = "{\"appSecret\":\"youxing\",\"chargeType\":1,\"cityCode\":\"330106\",\"driverId\":\"422803\",\"feeElectric\":1605,\"feeService\":0,\"feeTotal\":1605,\"orderId\":\"10024170614161044\",\"outOrderId\":\"201706141607413657\",\"power\":40,\"soc\":0,\"status\":2,\"stubId\":\"10024\",\"timeCharge\":4290000,\"timeEnd\":\"2017-06-14 17:22:14\",\"timeStart\":\"2017-06-14 16:10:44\"}";
        String packageSing = md5Encrypt(a, "utf-8", 0);
        String packageSing1 = md5Encrypt(a, "utf-8", 1);
        String packageSing2 = md5Encrypt(a, "utf-8", 2);
        System.out.println("packageSing === >>>  " + packageSing);
        System.out.println("packageSing1 === >>>  " + packageSing1);
        System.out.println("packageSing2 === >>>  " + packageSing2);
        System.out.println(packageSing == packageSing1 || packageSing1  == packageSing2);
    }

    public static String md5Encrypt(String input, String codeType, int b) {
        String md5;
        try {
            byte[] a = (codeType == null || codeType.length() == 0) ? input.getBytes() : input.getBytes(codeType);
            switch (b) {
                case 0 : md5 = MD5(a);
                    break;
                case 1 : md5 = a_MD5(a);
                    break;
                case 2 : md5 = b_MD5(input);
                    break;
                default: md5 = null;
                    break;
            }
        } catch (UnsupportedEncodingException e) {
            md5 = null;
            e.printStackTrace();
        }
        return md5;
    }

    public final static String MD5(byte[] bytes) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        try {
            byte[] btInput = bytes;
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public final static String a_MD5(byte[] bytes) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] btInput = bytes;
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String b_MD5(String plainText) {
        String re_md5 = new String();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes("utf-8"));
            byte b[] = md.digest();

            int i;

            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }

            re_md5 = buf.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return re_md5;
    }
}
